package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/26 14:22
 * @Version 1.0
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        //  orderId,paymentType 看做是一个联合主键
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getPaymentType,paymentType);
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderInfo.getId());

        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
        if (null != paymentInfoQuery){
            return;
        }
        //  创建一个paymentInfo 对象
        PaymentInfo paymentInfo = new PaymentInfo();
        //  赋值：
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        //  新增交易记录.
        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo,String paymentType) {
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getPaymentType,paymentType);
        return paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
    }

    @Override
    public void updatePaymentStatus(Long id, HashMap<String, String> paramsMap) {
        //  paymentInfo.getOrderId(); orderInfo.getId();
        //  找到outTradeNo;
        String outTradeNo = paramsMap.get("out_trade_no");
        PaymentInfo paymentInfoQuery = this.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
        if (null==paymentInfoQuery){
            return;
        }
        //  修改交易记录.
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setId(id);
        paymentInfo.setTradeNo(paramsMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramsMap.toString());
        //  修改交易记录.
        this.paymentInfoMapper.updateById(paymentInfo);

        //  发送一个消息给订单。
        this.rabbitService.sendMsg(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());
    }

    @Override
    public void updatePaymentStatus(String outTradeNo, PaymentInfo paymentInfo) {
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOutTradeNo,outTradeNo);
        this.paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);


    }

    @Override
    public void closePaymentInfo(Long orderId) {
        //  更新交易记录状态.
        //  update payment_info set payment_status = ? where order_id = orderId;
        //        UpdateWrapper<PaymentInfo> paymentInfoUpdateWrapper = new UpdateWrapper<>();
        //        paymentInfoUpdateWrapper.set("payment_status",PaymentStatus.CLOSED.name());
        //        paymentInfoUpdateWrapper.eq("order_id",orderId);
        //        paymentInfoMapper.update(null,paymentInfoUpdateWrapper);

        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderId,orderId);
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
        paymentInfoMapper.update(paymentInfo,paymentInfoLambdaQueryWrapper);
    }
}
